package com.example.zzl.viewmodel.HomeFragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.example.zzl.adapter.HomeFragment.SpecialAdapter;
import com.example.zzl.adapter.RecyclerViewBaseAdapter;
import com.example.zzl.databinding.FragmentSessionBinding;
import com.example.zzl.model.HomeFragment.SpecialModel;
import com.example.zzl.model.bean.Home;
import com.example.zzl.view.activity.SpecialInfoActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ligg on 2017/2/10.
 */

public class SpecialPresenter implements SpecialModel.SpecialCallbackM, RecyclerViewBaseAdapter.OnItemClickLitener {

    private final SpecialModel model;
    private Context context;
    private SpecialCallbackMv callback;
    private FragmentSessionBinding db;
    private List<Home.Special.ResultBean> list;
    private SpecialAdapter adapter;

    public SpecialPresenter(Context context, SpecialCallbackMv callback) {
        this.context = context;
        this.callback = callback;
        model = new SpecialModel(context, this);
    }

    @Override
    public void onItemClick(View view, int position) {
        Home.Special.ResultBean resultBean = list.get(position);
        Bundle bundle = new Bundle();
        bundle.putString("headTitle", resultBean.strSpecialName);
        bundle.putInt("iSpecialID", resultBean.iSpecialID);
        callback.startActivity(SpecialInfoActivity.class, bundle);
    }

    @Override
    public boolean onItemLongClick(View view, int position) {
        return false;
    }

    public interface SpecialCallbackMv {
        void startActivity(Class cls, Bundle bundle);
    }

    public void init() {
        list = new ArrayList<Home.Special.ResultBean>();
        adapter = new SpecialAdapter(context, list);
        adapter.setOnItemClickLitener(this);
    }

    public void initView() {
        db.rv.setLayoutManager(new LinearLayoutManager(context));
        db.rv.addItemDecoration(new android.support.v7.widget.DividerItemDecoration(context, android.support.v7.widget.DividerItemDecoration.VERTICAL));
        db.rv.setAdapter(adapter);
    }

    @Override
    public void loadFinish() {
        list.clear();
        list.addAll(model.getList());
        Log.e("SpecialPresenter", "loadFinish: ");
        adapter.notifyDataSetChanged();
    }

    public FragmentSessionBinding getDb() {
        return db;
    }

    public void setDb(FragmentSessionBinding db) {
        this.db = db;
    }

    public void loadData() {
        model.loadData();
    }
}
